#include "DevilSprite.h"

//Klasa przechowywujaca Kod bohatera
DevilSprite* DevilSprite::utworz() {
	DevilSprite *d = DevilSprite::create();
	return d;
}

bool DevilSprite::init() {
	this->initWithFile("devil_1.png");
	//this->initWithFile("DevilWithBck.png");

	//level = this->getContentSize().height / 2;

	CCSize size = CCDirector::sharedDirector()->getWinSize();
	level = size.height / 2;
	levelWidht = size.width;
	this->setPosition(ccp(30.0f,level));
	startTexture = this->getTexture();
	CCAnimation *devilAnimation = CCAnimation::create();
	//devilAnimation->addSpriteFrameWithFileName("DevilWithBck.png");
	//	devilAnimation->addSpriteFrameWithFileName("DevilWithBck.png");
	devilAnimation->addSpriteFrameWithFileName("devil_2.png");
	devilAnimation->addSpriteFrameWithFileName("devil_3.png");
	devilAnimation->addSpriteFrameWithFileName("devil_4.png");
	devilAnimation->addSpriteFrameWithFileName("devil_5.png");
	devilAnimation->addSpriteFrameWithFileName("devil_6.png");
	devilAnimation->addSpriteFrameWithFileName("devil_7.png");
	devilAnimation->setRestoreOriginalFrame(true);
	devilAnimation->setDelayPerUnit(0.5f);

	CCAnimate *devilAnimAction = CCAnimate::create(devilAnimation);
	devilAnimAction->setDuration(0.5f);
	devilRpeatAnimation = CCRepeatForever::create(devilAnimAction);
	devilRpeatAnimation->setTag(2);
	this->runAction(devilRpeatAnimation);
	currentlyJumping = false;
	falling=false;
	alive=true;
	won=false;
	return true;
}
void DevilSprite::moveCharacter(bool jump, CCPoint velocity, float deltaTime,
		bool onBar) {
	if (!onBar && !currentlyJumping) {
		if(falling){
			return; //powinien spasc ale juz spada
		}
		//CCLog("powinien spac %d %d",onBar, currentlyJumping);
		CCPoint oldPosition = this->getPosition();
		float x = this->getPosition().x;
		CCPoint newPosition = ccp(x,0.0f);
		CCMoveTo  *fall= CCMoveTo::create(0.9f, newPosition);
		CCFiniteTimeAction* actionMoveDone = CCCallFuncN::create(this,callfuncN_selector(DevilSprite::fallEnded));
		falling=true;
		fallAction=fall;
		this->runAction(CCSequence::create(fall, actionMoveDone, NULL));
	} else {
		CCPoint oldPosition = this->getPosition();
		CCPoint newPosition;
		if (jump) {
			//CCLog("skaczemy");
			if (currentlyJumping)
				return;
			this->setPositionY(oldPosition.y+20.0f);
			CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
			newPosition = ccp(screenSize.width*0.2f,-20.0f);

			if (this->isFlipX()) {
				newPosition = ccp(newPosition.x*-1.0f,newPosition.y);
			}

			if ((oldPosition.x + newPosition.x) < maxLeft) {
				newPosition.x = +40.0f;
			}

			if ((oldPosition.x + newPosition.x) >= maxRight) {
				newPosition.x = -40.0f;
			}

			CCJumpBy *jump = CCJumpBy::create(0.9f, newPosition, 200.0f, 1);
			jump->setTag(1);
			jumpAction=jump;
			CCFiniteTimeAction* actionMoveDone = CCCallFuncN::create(this,
					callfuncN_selector(DevilSprite::jumpEnded));
			currentlyJumping = true;
			 this->runAction(CCSequence::create(jump, actionMoveDone, NULL));
		} else {
			//CCLog("chodzimy");
			this->resumeSchedulerAndActions();
			CCPoint scaledVelocity = ccpMult(velocity, 1024.0f);
			if (currentlyJumping) {
				newPosition =
						ccp(oldPosition.x + scaledVelocity.x * deltaTime, oldPosition.y);
			} else {
				newPosition =
						ccp(oldPosition.x + scaledVelocity.x * deltaTime, level);
			}
			if (oldPosition.x != newPosition.x) {
				if (oldPosition.x > newPosition.x) {
					this->setFlipX(true);
				} else {
					this->setFlipX(false);
				}
			} else {
				if (!currentlyJumping) {
					this->pauseSchedulerAndActions();
					this->setTexture(startTexture);
				}
			}
			if (newPosition.x < maxLeft) {
				newPosition.x = maxLeft;
			}
			if (newPosition.x >= maxRight) {
				newPosition.x = maxRight;
			}
			this->setPosition(newPosition);
			if((newPosition.x+90.0f)>levelWidht){
				won=true;
			}
		}
	}

}
void DevilSprite::collisionWithBar(BelkaSprite* bar) {
	if(falling){
	//	CCLog("Force stop falling");
		fallAction->stop();
		falling=false;
	}

	if(currentlyJumping){
		//CCLog("start Force stop jumping");
		jumpAction->stop();
		currentlyJumping = false;
	}
	//gdzie jest dol bohatera
	float dy = this->getPositionY() - (this->getContentSize().height / 2);
	//gdzie jest gora polki
	float by = bar->getPositionY() - (bar->getContentSize().height / 2);
	//CCLog("dy= %f  by= %f",dy,by);
	if (dy < by) {
		float dx = this->getPositionX();
		float bx = bar->getPositionX();
		if (dx > bx) {
			//bohater po prawej stronie
			maxLeft = dx;
		} else {
			//bohater po lewej stronie
			maxRight = dx;
		}
	//	CCLog("zderzenie z polka");
	} else {
	//	CCLog("z gory");
		this->setLevel(by);
	}
}
void DevilSprite::jumpEnded() {
	//CCLog("jump ended");
	currentlyJumping = false;
}
void DevilSprite::fallEnded() {
	falling=false;
	float dbckc = (this->getPositionY()-(this->getContentSize().height/2));
	if(dbckc<1.0f)
		kill();
	//CCLog("fall ended should die %f",dbckc);
}
void DevilSprite::setLevel(float y) {
	level = y + (this->getContentSize().height / 2);
	this->setPositionY(level);
}
void DevilSprite::setLevelWidth(float y) {
	levelWidht = y;
}
void DevilSprite::setMaxLeftRight(float l, float r) {
	maxLeft = l;
	maxRight = r;
}
bool DevilSprite::isAlive()
{
	return alive;
}
void DevilSprite::kill()
{
	 alive=false;
}
bool DevilSprite::isWon()
{
	return won;
}
